<script setup>
const props = defineProps({
  disabled: {
    type: Boolean,
    default: false
  }
})
import {ref} from "vue";
import {http} from "@/api";
import {ElMessage} from "element-plus";
import useTable from "@/utils/useTable";
const {
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openDelete
} = useTable({
  requestFn: http.pvCoordinationRecord,
  params: {
    type: 'day'
  }
})
const formRef4 = ref(null)
const formData4 = ref({
  id: undefined,
  name: '',
  grid_command: '',
  photovoltaic_dispatch_command: '',
  type: 'day'
})
const rules4 = ref({
  name: [
    { required: true, message: '请输入', trigger: 'blur' }
  ],
  grid_command: [
    { required: true, message: '请输入', trigger: 'blur' }
  ],
  photovoltaic_dispatch_command: [
    { required: true, message: '请输入', trigger: 'blur' }
  ],
})
const editDateData = (row) => {
  formData4.value = row
  show.value = true
}
// 弹窗
const show = ref(false)
const addDateData = () => {
  formData4.value = {
    id: undefined,
    name: '',
    grid_command: '',
    photovoltaic_dispatch_command: '',
    type: 'day'
  }
  show.value = true
}
const submit4 = () => {
  formRef4.value.validate((valid) => {
    if (valid) {
      console.log('submit!')
      if (formData4.value.id) {
        http.pvCoordinationRecord.update(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      } else {
        http.pvCoordinationRecord.create(formData4.value).then(res => {
          ElMessage.success(res.msg)
          show.value = false
          search()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const reset4 = () => {
  show.value = false
}
</script>

<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <span>日前预测数据</span>
      </div>
    </template>
    <div v-if="!disabled" class="f-r f-e" style="margin-bottom: 10px">
      <el-button type="primary" @click="addDateData">添加</el-button>
    </div>
    <div>
      <el-table height="300px" :data="dataSource">
        <el-table-column prop="name" label="时间"></el-table-column>
        <el-table-column prop="grid_command" label="电网指令"></el-table-column>
        <el-table-column prop="photovoltaic_dispatch_command" label="光储调度指令"></el-table-column>
        <el-table-column label="操作" v-if="!disabled">
          <template #default="{row}">
            <el-button type="text" @click="editDateData(row)">编辑</el-button>
            <el-button type="text" @click="openDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </el-card>
  <el-dialog v-model="show" title="新增/编辑" width="500px">
    <el-form ref="formRef4" :model="formData4" :rules="rules4" label-width="120px" label-position="top">
      <el-form-item label="时间" prop="name">
        <el-date-picker v-model="formData4.name" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>

      </el-form-item>
      <el-form-item label="电网指令" prop="grid_command">
        <el-input v-model="formData4.grid_command" ></el-input>
      </el-form-item>
      <el-form-item label="光储调度指令" prop="photovoltaic_dispatch_command">
        <el-input v-model="formData4.photovoltaic_dispatch_command" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset4">取消</el-button>
        <el-button type="primary" @click="submit4">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>